package com.qixin.part.serviceimpl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.qixin.part.bean.PtUser;
import com.qixin.part.mapper.PtUserMapper;
import com.qixin.part.service.PtUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.util.cache.EhcacheUtil;
import com.qixin.util.sms.SmsMark;
import com.qixin.util.sms.SmsUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 薛向毅
 * @since 2018-09-28
 */




@Service
public class PtUserServiceImpl extends ServiceImpl<PtUserMapper, PtUser> implements PtUserService {

//企业登陆
    @Override
    public boolean login(PtUser ptUser, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_name",ptUser.getUserName());
        map.put("pass_word", ptUser.getPassWord());
        List<PtUser> PtUsers = this.selectByMap(map);
        //如果用户存在，放入session域
        if(PtUsers.size()>0) {
            session.setAttribute("user", PtUsers.get(0));
            return  true;
        }else {
            return false;
        }
    }



//
//    @Override
//    public String SendSMS(String userName) {
//        SendSmsResponse response;
//        String code= SmsMark.getCode();

//        try {
//            response =  SmsUtil.sendSms(userName+"", SmsMark.SMS_MARK,code);
//        } catch (ClientException e) {
//            e.printStackTrace();
//            return  "服务器出错，请稍后再试";
//        }
        //验证码获取成功后
//        if(response.getMessage().equals("OK")){
            //存进cache缓存  180后过期  当访问过一次后 60秒内没有再此访问 同样过期 缓存区的key设置成数字加字符串 否则获取不到

//        EhcacheUtil.put("SMS",userName+"Data",code);
//        String cacheCode=EhcacheUtil.get("SMS",userName+"Data")+"";
//        System.out.println("缓存中的验证码1："+cacheCode);
//        return  "获取验证码成功";

//        }
//        return "获取短信验证码失败";
//    }

//    @Override
//    public boolean equalsCache(String verification, Long userName) {
//        //从缓存中获取六位验证码 当访问过一次后 60秒内没有再此访问会过期  缓存区的key设置成数字加字符串 否则获取不到
//        String cacheCode=EhcacheUtil.get("SMS",userName+"Data")+"";
//        System.out.println("缓存中的验证码："+cacheCode);
//        System.out.println("用户输入的验证码："+verification);
//        System.out.println("用户手机号："+userName);
//        boolean b=verification.equals(cacheCode);
//        System.out.println("b="+b);
//        if (b){
//            return true;
//        }
//        return false;
//    }


//企业注册
//    @Override
//    public boolean registerUser(PtUser ptUser) {
//        ptUser.setCreateTime(new Date().getTime());
//        return  this.insert(ptUser);
//    }
}
